/**
 * controller_motto.js
 * 幸福语录控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-05-20
 */

//语录列表
app.controller('MottoList', function($scope, $http, common){
	//变量声明
	$scope.list 	= new Array();
	$scope.nPage 	= 1;
	$scope.pageHtml = '';
	$scope.nItem 	= new Object();
	$scope.aItem 	= new Object();
	$scope.loading 	= false;

	//初始化JS
	$scope.init_js 	= function(){
		$('#upload-picture-add, #upload-audio-add, #upload-picture-update, #upload-audio-update').uploadifive({
			'auto'             	: true,
			'uploadScript'     	: config.prefix_url+'/Public/upload',
			'removeCompleted'	: true,
			'onUploadComplete' 	: function(file, info) {
				var domId 	= $(this).attr('id');
				var info 	= eval('('+info+')');
				var file 	= info.data.info;
				file.path 	= config.server_dir + file.path;
				$scope.$apply(function(){
					switch(domId){
						case 'upload-picture-add':
							$scope.aItem.picture 	= file.path;
						break;
						case 'upload-audio-add':
							$scope.aItem.audio 	= file.path;
						break;
						case 'upload-picture-update':
							$scope.nItem.picture 	= file.path;
						break;
						case 'upload-audio-update':
							$scope.nItem.audio 	= file.path;
						break;
					}
				})
			}
		});

		$('.input-date').datetimepicker({
	        language:  'zh-CN',
	        format: 'yyyy-mm-dd hh:ii:ss',
	        weekStart: 1,
	        todayBtn:  1,
	        autoclose: 1,
	        todayHighlight: 1,
	        startView: 2,
	        forceParse: 0,
	        showMeridian: 1,
	        minView	: 0
	    });
	}

	//加载数据
	$scope.get_list 	= function(page){
		var page 	= page ? page : 1;
		var url 	= config.prefix_url + '/Motto/getList?p='+page;

		$http.get(url).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.list 	= info.data.list;
			$scope.nPage 	= page;
			$scope.pageHtml 	= info.data.page_html;
		})

		$scope.loading 	= true;
	}

	//添加条目
	$scope.add_item 	= function(item){
		var url 	= config.prefix_url + '/Motto/addItem';
		var param 	= item;

		$http.post(url, param).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.get_list($scope.nPage);
		})

		$scope.loading 	= true;
		$('.modal.add-motto').modal('hide');
	}

	//获取条目
	$scope.get_item 	= function(item){
		$scope.nItem 	= item;
		$('.modal.get-motto').modal('show');
	}

	$scope.update_item 	= function(id, info){
		var url 	= config.prefix_url + '/Motto/updateItem';
		var param 	= {
			'motto_id'	: id,
			'motto_info'	: info
		}
		console.log(param);
		$http.post(url, param).success(function(info){
			console.log(info);
			$scope.loading 	= false;
			if(info.code != 1){
				common.dealError(info);
				return false;
			}

			$scope.get_list($scope.nPage);
		})

		$scope.loading 	= true;
		$('.modal.get-motto').modal('hide');
	}

	$scope.get_list(1);
	$scope.init_js();
})